package com.cg.controller;


import com.cg.dto.*;
import com.cg.entity.Inheritor;
import com.cg.result.R;
import com.cg.service.IInheritorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 加
 * @since 2025-01-17
 */
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/api/inheritor")
@Api(value = "InheritorController",description = "欢迎来到传人管理",tags = "传人管理")
public class InheritorController {
    @Autowired
    private  IInheritorService inheritorService;
    @Autowired
    private FileController fileController;

        @ApiOperation("新增传人并上传图片")
        @PostMapping(value = "/addInheritor", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public R addInheritor(@RequestPart("inheritorName") String inheritorName,
                              @RequestPart("picture") MultipartFile picture,
                              @RequestPart("intro") String intro) throws IOException {

            // 处理文件上传
            String picturePath = fileController.upload(picture);
            System.out.println("图片路径：  "+picturePath);

            // 创建 DTO 对象
            AddInheritorDTO addInheritorDTO = new AddInheritorDTO();
            addInheritorDTO.setInheritorName(inheritorName);
            addInheritorDTO.setPicture(picturePath);
            addInheritorDTO.setIntro(intro);

            // 调用服务层
            return inheritorService.addInheritor(addInheritorDTO);
        }


    @ApiOperation("新增收藏")
    @PostMapping("addLike")
    public R addLike(AddLikeDTO addLikeDTO){
        return inheritorService.addLike(addLikeDTO);
    }


    @ApiOperation("修改传人信息")
    @PutMapping("/updateInheritor")
    public R updateInheritor(@Validated @RequestBody UpdateInheritorDTO updateInheritorDTO){
        return inheritorService.updateInheritor(updateInheritorDTO);
    }

    @ApiOperation("条件查询传人信息")
    @PostMapping("getInheritorByCondition")
    public R<List<Inheritor>> getInheritorByCondition(@Validated @RequestBody SearchInheritorDTO searchInheritorDTO){
        return  inheritorService.searchInheritorByCondition(searchInheritorDTO);
    }

    @ApiOperation("获取传人详情")
    @PostMapping("getInheritorDetail")
    public R getInheritorDetail(@RequestParam("inheritorId") Long inheritorId){
        return inheritorService.getInheritorDetail(inheritorId);
    }

}
